// loading框设置局部刷新，且所有请求完成后关闭loading框
import { ElLoading } from 'element-plus'
let loadingInstance: any = null
let needLoadingRequestCount = 0 // 声明一个对象用于存储请求个数
// 创建
function startLoading(): void {
  loadingInstance = ElLoading.service({
    fullscreen: false,
    text: '请求数据中...',
    background: 'rgba(255, 255, 255, 0.6)',
    target: '#mainBaseId',
  })
}
// 销毁
function endLoading(): void {
  if (loadingInstance) {
    loadingInstance.close()
  }
}
// 展示
function showFullScreenLoading(): void {
  if (needLoadingRequestCount === 0) {
    startLoading()
  }
  needLoadingRequestCount++
}
// 取消
function tryHideFullScreenLoading(): void {
  if (needLoadingRequestCount <= 0) return
  needLoadingRequestCount--
  if (needLoadingRequestCount === 0) {
    endLoading()
  }
}

export { showFullScreenLoading, tryHideFullScreenLoading }
